package com.msc.mobilecomputing;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.msc.mobilecomputing.adapters.TravelLocationAdapter;
import com.msc.mobilecomputing.model.TravelLocation;
import com.msc.mobilecomputing.utils.DataLoader;
import com.msc.mobilecomputing.utils.FontUtils;

public class MainActivity extends Activity {

	private ListView listView;
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		this.context = this;
		initUI();
		updateUI();
	}

	private void initUI() {
		listView = (ListView) findViewById(R.id.list_main);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				TravelLocation location = ((ArrayAdapter<TravelLocation>) listView
						.getAdapter()).getItem(position);
				startDescriptionActivity(location);

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle presses on the action bar items
		switch (item.getItemId()) {
		case R.id.action_about:
			openAbout();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void updateUI() {
		List<TravelLocation> travelLocations = DataLoader.getTravelLocations();
		TravelLocationAdapter adapter = new TravelLocationAdapter(this,
				R.layout.main_list_row, travelLocations);
		listView.setAdapter(adapter);
	}

	private void startDescriptionActivity(TravelLocation location) {
		Intent intent = new Intent(this, DescriptionActivity.class);
		intent.putExtra(DescriptionActivity.INTENT_EXTRA_LOCATION, location);
		startActivity(intent);
	}
	
	private void openAbout()
	{
		final AlertDialog aboutDialog = new AlertDialog.Builder( context ).create();

		LayoutInflater inflater = LayoutInflater.from( context );
		View pinPopupView = inflater.inflate( R.layout.pop_about, null );
		
		Button button = (Button) pinPopupView.findViewById(R.id.button_ok);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				aboutDialog.dismiss();
			}
		});

		aboutDialog.setView( pinPopupView );

		FontUtils.setCustomFont( pinPopupView, context.getAssets() );

		aboutDialog.show();
	}

}
